﻿using ProjectManager.Interface;
using ProjectManager.Logic.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectManager.UnderLogic;
using System.Data;

namespace ProjectManager.Controller
{
    internal class SqlController : ISql
    {
        private static object lockObject = new object();
        public void AddTask(ProjectInformation projectInformation, ProjectTaskInformation taskInformation)
        {
            string tablePath = projectInformation.TaskPath;
            string tableName = $"Task{taskInformation.ID}";
            string fileName = Path.Combine(tablePath, tableName + ".xml");
            string schemaFileName = Path.Combine(tablePath, tableName + ".xsd");

            DataTable dt = taskInformation.ToDataTable();
            dt.TableName = tableName;

            lock (lockObject)
            {
                dt.WriteXml(fileName);
                dt.WriteXmlSchema(schemaFileName);
            }
        }

        public void AddTeam(ProjectInformation projectInformation, TeamInformation teamInformation)
        {
            string tablePath = projectInformation.MemberPath;
            string tableName = $"Team{teamInformation.ID}";
            string fileName = Path.Combine(tablePath, tableName + ".xml");
            string schemaFileName = Path.Combine(tablePath, tableName + ".xsd");

            DataTable dt = teamInformation.ToDataTable();
            dt.TableName = tableName;

            lock (lockObject)
            {
                dt.WriteXml(fileName);
                dt.WriteXmlSchema(schemaFileName);
            }
        }

        public List<ProjectTaskInformation> GetTasks(ProjectInformation projectInformation)
        {
            string tablePath = projectInformation.TaskPath;
            string[] filePaths = Directory.GetFiles(tablePath);
            // 处理filePaths，删除所有后缀
            HashSet<string> files = [];
            foreach (string filePath in filePaths)
            {
                if (filePath.EndsWith(".xml") || filePath.EndsWith(".xsd"))
                {
                    files.Add(filePath[..filePath.LastIndexOf('.')]);
                }
            }

            List<DataTable> dataTables = [];

            lock (lockObject)
            {
                foreach (string filePath in files)
                {
                    // if (!File.Exists(filePath + ".xsd") || !File.Exists(filePath + ".xml")) continue;
                    DataTable dt = new();
                    dt.ReadXmlSchema(filePath + ".xsd");
                    dt.ReadXml(filePath + ".xml");
                    dataTables.Add(dt);
                }
            }
            return dataTables.ToTaskInfos();
        }

        public List<TeamInformation> GetTeams(ProjectInformation projectInformation)
        {
            string tablePath = projectInformation.MemberPath;
            string[] filePaths = Directory.GetFiles(tablePath);
            // 处理filePaths，删除所有后缀
            HashSet<string> files = filePaths.Select(x => x[..x.LastIndexOf('.')]).ToHashSet();
            List<DataTable> dataTables = [];

            lock (lockObject)
            {
                foreach (string filePath in files)
                {
                    if (!File.Exists(filePath + ".xsd") || !File.Exists(filePath + ".xml")) continue;
                    DataTable dt = new();
                    dt.ReadXmlSchema(filePath + ".xsd");
                    dt.ReadXml(filePath + ".xml");
                    dataTables.Add(dt);
                }
            }
            var tasks = GetTasks(projectInformation);
            return dataTables.ToTeamInfos(tasks);
        }

        public void UpdateTask(ProjectInformation projectInformation, ProjectTaskInformation taskInformation)
        {
            string tablePath = projectInformation.TaskPath;
            string filePath = Path.Combine(tablePath, $"Task{taskInformation.ID}");
            DataTable dt = taskInformation.ToDataTable();
            dt.TableName = $"Task{taskInformation.ID}";

            lock (lockObject)
            {
                dt.WriteXml(filePath + ".xml", XmlWriteMode.WriteSchema);
                dt.WriteXmlSchema(filePath + ".xsd");
            }
        }

        public void UpdateTeam(ProjectInformation projectInformation, TeamInformation teamInformation)
        {
            string tablePath = projectInformation.MemberPath;
            string filePath = Path.Combine(tablePath, $"Team{teamInformation.ID}");
            DataTable dt = teamInformation.ToDataTable();
            dt.TableName = $"Team{teamInformation.ID}";

            lock (lockObject)
            {
                dt.WriteXml(filePath + ".xml", XmlWriteMode.WriteSchema);
                dt.WriteXmlSchema(filePath + ".xsd");
            }
        }
    }
}
